package com.mall.server.controller.yxcontroller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.SkuService;
import com.tanhua.model.domain.PmsSkuStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;


    /**
     * 根据商品ID及sku编码模糊搜索sku库存
     * @param pid
     * @param keyword
     * @return
     */
    @GetMapping("/{pid}")
    public R findByPid(@PathVariable("pid")Long pid,String keyword){
        if (pid == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        List<PmsSkuStock> list= skuService.findByPid(pid,keyword);
        return R.success(list);
    }

    /**
     * 批量更新sku库存信息
     * @param pid
     * @param skuStockList
     * @return
     */
    @PostMapping("/update/{pid}")
    public R updateById(@PathVariable("pid")Long pid,@RequestBody List<PmsSkuStock> skuStockList){

        if (pid == null || skuStockList == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        skuService.updateByPid(pid,skuStockList);
        return R.success("");
    }
}
